package com.bolt.admin.logging.util;

import com.bolt.common.utils.StrUtil;
import org.slf4j.MDC;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2020/2/14.
 */
public class TraceUtil {
    public static String getTrace() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
                .getRequest();

        String app_trace_id = request.getHeader(TraceConstant.HTTP_HEADER_TRACE_ID);

        //未经过HandlerInterceptor的设置
        if (StrUtil.isBlank(MDC.get(TraceConstant.LOG_TRACE_ID))) {
            //但是有请求头，重新设置
            if (StrUtil.isNotEmpty(app_trace_id)) {
                MDC.put(TraceConstant.LOG_TRACE_ID, app_trace_id);
            }
        }

        return app_trace_id;

    }
}

 